package com.example.recuitactivity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.recuitactivity.bean.Interview;
import com.example.recuitactivity.bean.Resume;
import com.example.recuitactivity.helper.DbOpenHelper;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class SearchResumeActivity extends AppCompatActivity {
    private Button btn_interview;
    private TextView tv_name,tv_sex,tv_school,tv_age,tv_tele,tv_edubg,tv_project_exp,tv_reward_exp;
    private Bundle bundle;
    private DbOpenHelper dbOpenHelper;

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.btn_send_interview:
                    showInterviewDialog();
                    break;
            }
        }
    };

    public void initUI() {
        btn_interview=(Button) findViewById(R.id.btn_send_interview);
        tv_name=(TextView) findViewById(R.id.tv_search_resume_name);
        tv_sex=(TextView) findViewById(R.id.tv_search_resume_sex);
        tv_school=(TextView) findViewById(R.id.tv_search_resume_school);
        tv_age=(TextView) findViewById(R.id.tv_search_resume_age);
        tv_tele=(TextView) findViewById(R.id.tv_search_resume_tele);
        tv_edubg=(TextView) findViewById(R.id.tv_search_resume_edubg);
        tv_project_exp=(TextView) findViewById(R.id.tv_search_resume_project_exp);
        tv_reward_exp=(TextView) findViewById(R.id.tv_search_resume_reward_exp);

        btn_interview.setOnClickListener(listener);
    }

    public void initDates(){
        dbOpenHelper=new DbOpenHelper(this);
        bundle=getIntent().getExtras();
        long candidates_id=bundle.getLong("cid");
        Resume resume= dbOpenHelper.getResumeInfoById(candidates_id);

        tv_name.setText(resume.getName());
        tv_sex.setText(resume.getSex());
        tv_school.setText(resume.getSchool());
        tv_age.setText(resume.getAge()==0? "":String.valueOf(resume.getAge()));
        tv_tele.setText(resume.getTelephone());
        tv_edubg.setText(resume.getEdu_background());
        tv_project_exp.setText(resume.getProject_exp());
        tv_reward_exp.setText(resume.getReward_exp());
    }

    public void showInterviewDialog(){
        View view= LayoutInflater.from(this).inflate(R.layout.send_interview_info,null,false);
        final AlertDialog dialog=new AlertDialog.Builder(this).setView(view).create();
        EditText et_time=(EditText) view.findViewById(R.id.et_edit_interview_time);
        EditText et_tips=(EditText) view.findViewById(R.id.et_edit_interview_tips);
        Button btn_send=(Button) view.findViewById(R.id.btn_edit_interview_send);

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String interview_time=et_time.getText().toString();
                String interview_tips=et_tips.getText().toString();

                if(interview_time.length()==0||interview_tips.length()==0){
                    Toast.makeText(SearchResumeActivity.this,"面试信息不能为空",Toast.LENGTH_SHORT).show();
                }else{
                    long cid=bundle.getLong("cid");
                    long eid=bundle.getLong("eid");
                    long pid=bundle.getLong("pid");
                    String candidateName=dbOpenHelper.getResumeInfoById(cid).getName();
                    String postName=bundle.getString("postname");
                    String enterpriseName=dbOpenHelper.getEnterpriseCompanyById(eid);
                    String sendTime=getCurrentTime();
                    dbOpenHelper.insertInterview(new Interview(cid,candidateName,postName,enterpriseName,interview_time,interview_tips,sendTime));
                    dbOpenHelper.updateStateById(cid,eid,pid,"面试通知");//更新投递状态
                    Toast.makeText(SearchResumeActivity.this,"发送成功!",Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            }
        });

        dialog.show();
    }

    public String getCurrentTime(){
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return dateFormat.format(calendar.getTime());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_resume);
        ActionBar actionBar=getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        initUI();
        initDates();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return false;
    }
}